package spring.cloud.tasks.tasks_assign_manager.task_item_assign.pick_up.task;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import spring.cloud.tasks.common.node.TaskNodePath;
import spring.cloud.tasks.common.task_item_allot.OnlineExecutor;
import spring.cloud.tasks.common.task_item_allot.TaskItem;
import spring.cloud.tasks.tasks_assign_manager.domain.WaitedAssignTaskInfo;
import spring.cloud.tasks.tasks_assign_manager.service.TaskItemAssignService;
import spring.cloud.tasks.tasks_assign_manager.task_item_assign.PreferExecutorSettings;
import spring.cloud.tasks.tasks_assign_manager.task_item_assign.TaskItemAssignUtils;
import spring.cloud.tasks.tasks_assign_manager.task_item_assign.pick_up.PickUp;

import java.util.List;

/**
 * 任务重排，移除所有executor的该任务shard，重新获取该任务的shards，finally删除forceShard结点
 */
@Slf4j
@Setter
@Getter
public class TaskOneKeyTaskItemAssign extends PickUp {
    private String taskId;

    public TaskOneKeyTaskItemAssign(TaskItemAssignService taskItemAssignService) {
        super(taskItemAssignService);

    }

    @Override
    protected void log() {
        log.info("Execute the {} with {} forceShard", this.getClass().getSimpleName(), taskId);
    }

    @Override
    public boolean pickUp(CuratorFramework curatorFramework,//
                          List<String> taskIdList,//
                          List<String> enableTaskIdList,//
                          List<TaskItem> pickUpEdTaskItemList,//
                          List<TaskItem> discardTaskItemList,//
                          List<OnlineExecutor> onlineExecutorList,//
                          List<OnlineExecutor> ownTrafficOnlineExecutorList,//
                          PreferExecutorSettings preferExecutorSettings) throws Exception {
        try {
            //先移除清理
            TaskItemAssignUtils.removeTaskItemListOfTask(ownTrafficOnlineExecutorList, taskId);
            //获取支持该task的Executor
            TaskItemAssignUtils.refreshOnlineExecutorSupportedTaskIdList(curatorFramework, onlineExecutorList, taskId);
            //如果该任务是启用状态，重新创建
            if (enableTaskIdList.contains(taskId)) {
                WaitedAssignTaskInfo waitedAssignTaskInfo = new WaitedAssignTaskInfo();
                pickUpEdTaskItemList.addAll(TaskItemAssignUtils.createTaskItemList(curatorFramework, waitedAssignTaskInfo));
            }
        } finally {
            deleteForceShardNode(curatorFramework);
        }
        return true;
    }

    private void deleteForceShardNode(CuratorFramework curatorFramework) {
        try {
            String taskConfigForceShardNodePath = TaskNodePath.getTaskConfigForceShardNodePath(taskId);
            if (curatorFramework.checkExists().forPath(taskConfigForceShardNodePath) != null) {
                curatorFramework.delete().forPath(taskConfigForceShardNodePath);
            }
        } catch (Throwable t) {
            log.error("delete forceShard node error", t);
        }
    }

}
